import requests
import random
import time
import redis

base_url = 'http://124.223.139.46:30001/api'
headers = {
    'User-Agent': 'Apifox/1.0.0 (https://apifox.com)',
}

# 连接Redis
redis_conn = redis.Redis(host='124.220.186.165', password='ilcjals', port=6379)

for _ in range(1):
    # 生成234+10位数字随机手机号
    phone = '234' + f"{random.randint(1000000000, 9999999999)}"
    # 接口1：注册短信发送
    response = requests.post(
        f"{base_url}/users",
        headers=headers,
        data={'phone': phone}
    )
    print(phone)
    response_data = response.json()

    if response.status_code == 200 and response_data.get('send') is True:
        # 接口2：注册
        response = requests.post(
            f"{base_url}/user/verify",
            headers=headers,
            json={
                "country": "Nigeria",
                "first_name": "234",
                "invite_code": "invicode",
                "last_name": "567",
                "password": "qwe123",
                "promo_code": "",
                "type": "phone",
                "value": phone,
                "verification_code": "9999"
            }
        )
        response_data = response.json()
        access_token = response_data.get('auth', {}).get('access_token')
        print(access_token)
        # 插入手机号和access_token时同时维护一个手机号的列表
        redis_conn.sadd('phone_numbers', phone)
        redis_conn.set(phone, access_token)
    time.sleep(1)

